package discount;

import impl.Calculate;
import impl.Fruit;

/**
 * 折扣优惠工厂
 */
public class DisCountFactory {

    public Calculate getCalculate(String disCountName) {
       if (disCountName.equals("discount")){
           return new OperationDisCount();
       }else if (disCountName.equals("fullReduction")){
           return new OperationFullReduction();
       }
       return null;
    }


    // 计算所有购买水果需要多少钱
    public double calculatePrice(Fruit... fruits) {
        DisCountFactory disCountFactory = new DisCountFactory();
        FruitContext fruitContext = new FruitContext();

        double priceTotal = 0.0;
        for (Fruit fruit : fruits) {
            // 计算单个水果多少钱
            double fruitPrice = fruit.getPrice() * fruit.getKilogram();
            // 折扣优惠逻辑处理
            Enum<?> EnumValue = DiscountMap.getDiscountEnum(fruit.getName());
            fruitContext.setCalculate(disCountFactory.getCalculate("discount"));
            fruitPrice = fruitContext.calculateFruitPrice(fruitPrice, EnumValue);
            // 水果价格加起来
            priceTotal += fruitPrice;
        }
        // 满减优惠逻辑处理
        Enum<?> EnumValue = DiscountMap.getDiscountEnum("total");
        fruitContext.setCalculate(disCountFactory.getCalculate("fullReduction"));
        priceTotal = fruitContext.calculateFruitPrice(priceTotal, EnumValue);
        return priceTotal;
    }

}
